//
//  RegisterViewController.m
//  维度
//
//  Created by 宋梦华 on 2019/1/23.
//  Copyright © 2019年 宋梦华. All rights reserved.
//

#import "RegisterViewController.h"
#import "EnterViewController.h"
#import "AESCrypt.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //   背景图
    self.navigationController.navigationBar.hidden = YES;
    UIView *view =[[UIView alloc]initWithFrame:self.view.frame];
    UIImageView *img =[[UIImageView alloc]initWithFrame:self.view.frame];
    img.image =[UIImage imageNamed:@"Back"];
    [view addSubview:img];
    [self.view addSubview:view];
    
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 0.0;
    
    [self.view addSubview:self.NameTF];
    [self.view addSubview:self.SexTF];
    [self.view addSubview:self.BirthdayTF];
    [self.view addSubview:self.PhoneTF];
    [self.view addSubview:self.EmailTF];
    [self.view addSubview:self.EnterPasswordTF];
    [self.view addSubview:self.resignBtn];
    
}
-(UITextField *)NameTF{
    if (!_NameTF) {
        _NameTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 150, 350, 60)];
        //        设置提示文字的颜色
        NSString *holderText = @"昵称";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:20]
                            range:NSMakeRange(0, holderText.length)];
        _NameTF.attributedPlaceholder = placeholder;
        _NameTF.textAlignment = NSTextAlignmentCenter;
        //        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,50,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_NameTF addSubview:underLine];
        //        图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 25, 25)];
        img.image = [UIImage imageNamed:@"register_icon_name_default_hdpi"];
        [_NameTF addSubview:img];
    }
    return _NameTF;
}
-(UITextField *)SexTF{
    if (!_SexTF) {
        _SexTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 210, 350, 60)];
        //        设置提示文字的颜色
        NSString *holderText = @"性别";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:20]
                            range:NSMakeRange(0, holderText.length)];
        _SexTF.attributedPlaceholder = placeholder;
        _SexTF.textAlignment = NSTextAlignmentCenter;
        //        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,50,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_SexTF addSubview:underLine];
        //        图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 25, 25)];
        img.image = [UIImage imageNamed:@"register_icon_sex_default_hdpi"];
        [_SexTF addSubview:img];
    }
    return _SexTF;
}
-(UITextField *)BirthdayTF{
    if (!_BirthdayTF) {
        _BirthdayTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 270, 350, 60)];
        //        设置提示文字的颜色
        NSString *holderText = @"出生日期";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:20]
                            range:NSMakeRange(0, holderText.length)];
        _BirthdayTF.attributedPlaceholder = placeholder;
        _BirthdayTF.textAlignment = NSTextAlignmentCenter;
        //        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,50,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_BirthdayTF addSubview:underLine];
        //        图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 25, 25)];
        img.image = [UIImage imageNamed:@"register_icon_date_default_hdpi"];
        [_BirthdayTF addSubview:img];
    }
    return _BirthdayTF;
}
-(UITextField *)PhoneTF{
    if (!_PhoneTF) {
        _PhoneTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 330, 350, 60)];
        //        设置提示文字的颜色
        NSString *holderText = @"手机号";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:20]
                            range:NSMakeRange(0, holderText.length)];
        _PhoneTF.attributedPlaceholder = placeholder;
        _PhoneTF.textAlignment = NSTextAlignmentCenter;
        //        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,50,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_PhoneTF addSubview:underLine];
        //        图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 20, 25)];
        img.image = [UIImage imageNamed:@"register_icon_phone_default_hdpi"];
        [_PhoneTF addSubview:img];
    }
    return _PhoneTF;
}
-(UITextField *)EmailTF{
    if (!_EmailTF) {
        _EmailTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 390, 350, 60)];
        //        设置提示文字的颜色
        NSString *holderText = @"邮箱";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:20]
                            range:NSMakeRange(0, holderText.length)];
        _EmailTF.attributedPlaceholder = placeholder;
        _EmailTF.textAlignment = NSTextAlignmentCenter;
        //        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,50,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_EmailTF addSubview:underLine];
        //        图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 25, 25)];
        img.image = [UIImage imageNamed:@"register_icon_mail_default_hdpi"];
        [_EmailTF addSubview:img];
    }
    return _EmailTF;
}
-(UITextField *)EnterPasswordTF{
    if (!_EnterPasswordTF) {
        _EnterPasswordTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 450, 350, 60)];
        //        设置提示文字的颜色
        NSString *holderText = @"登录密码";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:20]
                            range:NSMakeRange(0, holderText.length)];
        _EnterPasswordTF.attributedPlaceholder = placeholder;
        _EnterPasswordTF.textAlignment = NSTextAlignmentCenter;
        //        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,50,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_EnterPasswordTF addSubview:underLine];
        //        图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 25, 25)];
        img.image = [UIImage imageNamed:@"register_icon_lock_default_hdpi"];
        [_EnterPasswordTF addSubview:img];
    }
    return _EnterPasswordTF;
}

//登录
-(UIButton *)resignBtn{
    if (!_resignBtn) {
        _resignBtn =[[UIButton alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 550, 350, 50)];
        [_resignBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_resignBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_resignBtn setBackgroundColor:[[UIColor alloc]initWithRed:255/255.0 green:209/255.0 blue:0/255.0 alpha:1.0]];
        [_resignBtn addTarget:self action:@selector(resign) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _resignBtn;
}
-(void)resign{
    //加密
    NSString *message = @"123";
    NSString *encryptedData = [AESCrypt encrypt:message password:self.EnterPasswordTF.text];
    
    NSLog(@"加密前的密码是 ----- %@",self.EnterPasswordTF.text);
    NSLog(@"加密后的密码是 ------%@",encryptedData);

    NSDictionary *dic = @{@"user_name":self.NameTF.text,
                          @"user_sax":self.SexTF.text,
                          @"user_data":self.BirthdayTF.text,
                          @"user_phone":self.PhoneTF.text,
                          @"user_email":self.EmailTF.text,
                        @"user_password":encryptedData
                          };
    
    
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manger POST:@"http://172.17.8.100/movieApi/user/v1/registerUser" parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"//////%@,注册成功",responseObject);
       NSLog(@"//////%@,注册成功",responseObject[@"message"]);
        dispatch_async(dispatch_get_main_queue(), ^{
            //后台返回注册成功同步跳转登录界面
            EnterViewController *enter = [EnterViewController new];
            [self.navigationController presentViewController:enter animated:YES completion:nil];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"注册失败");
    }];

    
}
@end
